<template>
    <div class = "login_container">
        <!-- 登录盒子 -->
        <div class = "login_box">
            <!-- 登录logo -->
            <div class = "avatar_box">
                <img alt = "" src = "@/assets/head.jpeg">
            </div>
            <!-- 登录表单区域 -->
            <el-form ref = "loginFormRef" :model = "loginForm" :rules = "loginFormRules" class = "login_form"
                     label-width = "0px">
                <!-- 用户名 -->
                <el-form-item prop = "username">
                    <el-input v-model = "loginForm.username" placeholder = "用户名"
                              prefix-icon = "el-icon-user-solid"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop = "password">
                    <el-input v-model = "loginForm.password" placeholder = "密码" prefix-icon = "el-icon-lock"
                              type = "password"></el-input>
                </el-form-item>
                <!-- 登录按钮 -->
                <el-form-item class = "btns">
                    <el-button type = "primary" @click = "login">登录</el-button>
                    <el-button type = "info" @click = "resetLoignForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
    name: "User-Login",
    data () {
        return {
            // 登录表单数据
            loginForm: {
                username: "admin",
                password: "123456"
            },
            // 表单校验规则对象
            loginFormRules: {
                // 验证用户名
                username: [
                    {required: true, message: "请输入登录名称", trigger: "blur"},
                    {min: 3, max: 10, message: "长度在3-10个字符", trigger: "blur"}
                ],
                // 验证密码
                password: [
                    {required: true, message: "请输入登录密码", trigger: "blur"},
                    {min: 6, max: 15, message: "长度在6-15个字符", trigger: "blur"}
                ]
            }
        };
    },
    methods: {
        /**
         * @name resetLoignForm()
         * @点击重置按钮 重置登录表单
         */
        resetLoignForm () {
            this.$refs.loginFormRef.resetFields()
        },
        /**
         * @name login()
         * @表单登录
         */
        login () {
            this.$refs.loginFormRef.validate(async (valid) => {
                if (!valid) return;
                // let res = await login({...this.loginForm});
                const {data: {meta: {status, msg}, data}} = await this.$http.post('/login', {
                    ...this.loginForm
                })
                if (status !== 200) {
                    this.$message.error(msg);
                } else {
                    this.$message.success('登录成功！');
                    // 保存token
                    window.sessionStorage.setItem('token', data.token);
                    this.$router.push("/home");
                }
            })
        }
    }
}
</script>
<style lang = "less" scoped>
.login_container {
    height: 100%;
    background-color: #2b4b6b;
    // 登录盒子
    .login_box {
        left: 50%;
        top: 50%;
        width: 450px;
        height: 300px;
        border-radius: 3px;
        position: absolute;
        background-color: #fff;
        transform: translate(-50%, -50%);
        // 登录logo
        .avatar_box {
            left: 50%;
            height: 100px;
            width: 100px;
            padding: 10px;
            position: absolute;
            border-radius: 50%;
            border: 1px solid #eee;
            transform: translate(-50%, -50%);
            box-shadow: 0 0 10px #ddd;
            background-color: #fff;
            // logo 图片
            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }

        // 登录表单
        .login_form {
            width: 100%;
            bottom: 20px;
            padding: 0 20px;
            position: absolute;
            box-sizing: border-box;
            // 登录按钮组
            .btns {
                display: flex;
                justify-content: flex-end;
            }
        }
    }
}
</style>
